//
//  BaseTableViewCell.m
//  YiXiu
//
//  Created by ZHY on 16/6/27.
//  Copyright © 2016年 zzz. All rights reserved.
//

#import "BaseTableViewCell.h"
#import "BaseCollectionViewCell.h"



@interface BaseTableViewCell ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property(nonatomic, strong)  CAKeyframeAnimation *keyAnimation;


@end

@implementation BaseTableViewCell



- (void)beginShake{
    for (UIView * view in self.collect.subviews) {
        if ([view isKindOfClass:[BaseCollectionViewCell class]]) {
            [view.layer removeAllAnimations];
            [view.layer addAnimation:self.keyAnimation forKey:nil];
        }
    }
}

- (void)endShake{
    for (UIView * view in self.collect.subviews) {
        if ([view isKindOfClass:[BaseCollectionViewCell class]]) {
            [view.layer removeAllAnimations];
        }
    }
}

- (CAKeyframeAnimation *)keyAnimation{
    if (!_keyAnimation) {
        CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
        
        keyAnimation.values = @[@(-M_1_PI/4), @(M_1_PI/4), @(-M_1_PI/4)];
        
        keyAnimation.repeatCount = CGFLOAT_MAX;
        
        keyAnimation.duration = 0.12;     //all animation's duration is 0.25
        _keyAnimation = keyAnimation;
    }
    return _keyAnimation;
}



- (void)awakeFromNib {
    [super awakeFromNib];
    
//    [self setBackgroundColor:[UIColor grayColor]];
    [self setBackgroundColor:[UIColor whiteColor]];
}


- (void)setArray:(NSMutableArray *)array{
    _array = array;
    
    CGFloat max = ceilf(array.count/4.0);
    CGFloat heigth = (max * itemheigth) + ((max-1) * interSpace)>0?(max * itemheigth) + ((max-1) * interSpace):itemheigth;
    self.collect.frame = CGRectMake(10, 20, collectWidth, heigth);
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self addSubview:self.collect];
    
}

-(UICollectionView *)collect{
    
    if (!_collect) {
        UICollectionViewFlowLayout * fl = [[UICollectionViewFlowLayout alloc] init];
        fl.itemSize = CGSizeMake(itemWidth, itemheigth);
        fl.minimumLineSpacing = 20;
        fl.minimumInteritemSpacing = 20;
        
        _collect = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:fl];
        [_collect registerNib:[UINib nibWithNibName:@"BaseCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
        _collect.backgroundColor = [UIColor clearColor];
        _collect.delegate = self;
        _collect.dataSource = self;
        UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handlelongGesture:)];
        [_collect addGestureRecognizer:longGesture];
        
    }
    return _collect;
}

- (void)handlelongGesture:(UILongPressGestureRecognizer *)longs{
    
    NSLog(@"%s",__func__);
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.array.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    BaseCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
   
    
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.isEdit) {
        [cell.layer removeAllAnimations];
        [cell.layer addAnimation:self.keyAnimation forKey:nil];
    }
     ((BaseCollectionViewCell *)cell).titleLabel.text = self.array[indexPath.row];
    
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (self.selectBlock) {
        self.selectBlock([collectionView cellForItemAtIndexPath:indexPath],indexPath,self.isEdit);
    }
}


//- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
//    
//    
//    [super setSelected:selected animated:animated];
//    
//    
//}


@end
